import Vue from 'vue'
import Vuex from 'vuex'
import * as Cookies from 'js-cookie'
import createPersistedState from 'vuex-persistedstate'
import createLogger from 'vuex/dist/logger'
import * as actions from './actions'
import * as getters from './getters'
import state from './state'
import mutations from './mutations'
import player from './modules/player/index'
Vue.use(Vuex)

// webpack 中 生产模式或开发
const debug = process.env.NODE_ENV !== 'production'
const store = new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
  modules: {
    player
  },
  // 严格模式
  strict: debug,
  plugins: [
    // vuex-persistedstate，使vuex状态持久化（缓存到本地）
    createPersistedState({
      storage: {
        getItem: key => Cookies.get(key),
        setItem: (key, value) => Cookies.set(key, value, {
          expires: 3,
          secure: true
        }),
        removeItem: key => Cookies.remove(key)
      }
    }),
    debug ? createLogger() : false
  ]
})

export default store
